#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "lib.h"

void array_fill_with_rand(int *array,int size)
{
	int i;
	
	if (array == NULL) {
		return;	
	}
	srand(time(NULL));	
	for (i=0; i<size;  i++) {
		array[i] = rand()%(size);
	}
}

void array_fill_with_rand_neg(int *array,int size)
{
	int i;
	
	if (array == NULL) {
		return;	
	}
	srand(time(NULL));	
	for (i=0; i<size;  i++) {
		array[i] = rand()%(size);
		if (i % 3 == 0) {
			array[i] = -array[i] ;
		}
		
	}
}
void array_fill_with_rand_only(int *array,int size)
{
	int i;
	int value;
	int j;
	
	if (array == NULL) {
		return;	
	}
	srand(time(NULL)+rand()+rand());	
	for (i=0; i<size;  i++) {
		while (1) {
			value = (rand())%size + 1;
			for (j=0; j<i;  j++) {
				if (array[j] == value) {
					srand(time(NULL)+i+j+value);	
					break;
				}
			}
			if (j != i) {
				continue;
			}
			array[i] = value;
			break;
		}
		
	}
}

void array_print(int *array,int size,char *info)
{
	int i;
	
	if (array == NULL) {
		return;	
	}
	if (info != NULL) {
		printf("%s ",info);
	}
	for (i=0; i<size;  i++) {
		printf("%02d ",array[i]);
	}
	printf("\n");
}
void array_print_char(char *array,int size,char *info)
{
	int i;
	
	if (array == NULL) {
		return;	
	}
	
	printf("ser ");
	for (i=0; i<size;  i++) {
		printf("%02d ",i);
	}
	printf("\n");
	
	if (info != NULL) {
		printf("%s ",info);
	}
	for (i=0; i<size;  i++) {
		printf("%c  ",array[i]);
	}
	printf("\n");
}
void array_all_type_print(void *a,int size,int val,char *info,char *type)
{
	int i;
	double *array = (double *)a;
	
	if (array == NULL || type == NULL) {
		return;	
	}
	if (info != NULL) {
		printf("%s ",info);
	}
	for (i=0; i<size;  i++) {
		printf(type,array[i]);
	}
	printf("\n");
}
ret_e printf_int(int data,void *context)
{
	printf("%d\t",data);
	context = context;
	
	return RET_OK;
}

void two_dimension_array_print(int *array,int m,int n) 
{
	int i;
	int j;
	char tmp[20];
	
	printf("\ni\\j ");
	for (j=0; j<n; j++) {
		printf("%02d ",j);
	}
	putchar('\n');
	for (i=0; i<m; i++) {
		snprintf(tmp,sizeof(tmp),"i%d ",i);
		array_print(array+i*n,n,tmp);
	}
}

int int_get_max(void)
{
	 unsigned  int max = (0-1);
	return (max>>1);
}
int int_get_min(void)
{
	unsigned int max = (0-1);
	return (0-(max>>1)-1);
}

void swap(int *array, int l,int r)
{
	int tmp = array[l];

	array[l] = array[r];
	array[r] = tmp;
}